import pandas as pd

# Load the CSV file into a DataFrame
file_path = 'merge_data1.csv'
df = pd.read_csv(file_path)

# Show the first few rows of the DataFrame to understand its structure
df.head()
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# Step 1: Filter records where '发货记录完成时间' contains '2023'
df['发货记录完成时间'] = pd.to_datetime(df['发货记录完成时间'])
df_2023 = df[df['发货记录完成时间'].dt.year == 2023]

# Step 2: Calculate the total amount for each customer
total_amount_by_customer = df_2023.groupby('客户名称')['货款'].sum().reset_index()
'''
# Step 3: Plot the data
plt.figure(figsize=(12, 6))
plt.barh(total_amount_by_customer['客户名称'], total_amount_by_customer['货款'], color='skyblue')
plt.xlabel('货款总和')
plt.ylabel('客户名称')
plt.title('2023年各客户货款总和')
plt.grid(axis='x')
plt.show()
'''
# Sort the DataFrame by '货款' in descending order and take the top 10 customers
top_10_customers = total_amount_by_customer.sort_values(by='货款', ascending=False).head(10)

top_10_customers

# Step 1: Filter records where '发货记录完成时间' contains '2023'
df_2023 = df[df['发货记录完成时间'].dt.year == 2023]

# Step 2: Calculate the total tons for each customer
total_tons_by_customer = df_2023.groupby('客户名称')['发货吨位'].sum().reset_index()

# Step 3: Sort the DataFrame by '发货吨位' in descending order and take the top 10 customers
top_10_customers_tons = total_tons_by_customer.sort_values(by='发货吨位', ascending=False).head(10)

# Step 4: Plot the data
plt.figure(figsize=(12, 6))
plt.barh(top_10_customers_tons['客户名称'], top_10_customers_tons['发货吨位'], color='green')
plt.xlabel('发货吨位总和')
plt.ylabel('客户名称')
plt.title('2023年前十名客户发货吨位总和')
plt.grid(axis='x')
plt.show()
# Filter records where '货品类型' is 0 and '货款' is not NaN
df_2023_goods_0 = df_2023[df_2023['货品类型'] == 0]
df_2023_goods_0 = df_2023_goods_0[df_2023_goods_0['货款'].notna()]

# Calculate the total amount for each customer
total_amount_by_customer_goods_0 = df_2023_goods_0.groupby('客户名称')['货款'].sum().reset_index()

# Sort the DataFrame by '货款' in descending order and take the top 1 customer
top_customer_goods_0 = total_amount_by_customer_goods_0.sort_values(by='货款', ascending=False).head(1)
'''
# Plot the data
plt.figure(figsize=(8, 6))
plt.barh(top_customer_goods_0['客户名称'], top_customer_goods_0['货款'], color='purple')
plt.xlabel('货款总和')
plt.ylabel('客户名称')
plt.title('货品类型为0的达成最高货款的客户 (2023年)')
plt.grid(axis='x')
plt.show()
'''
# Display the top 10 customers by total amount for goods type 0
top_10_customers_goods_0 = total_amount_by_customer_goods_0.sort_values(by='货款', ascending=False).head(10)
top_10_customers_goods_0
# Plot the top 10 customers by total amount for goods type 0
plt.figure(figsize=(12, 6))
plt.barh(top_10_customers_goods_0['客户名称'], top_10_customers_goods_0['货款'], color='orange')
plt.xlabel('货款总和')
plt.ylabel('客户名称')
plt.title('货品类型为0的2023年前十名客户货款总和')
plt.grid(axis='x')
plt.show()
# Filter records where '货品类型' is 1 and '货款' is not NaN
df_2023_goods_1 = df_2023[df_2023['货品类型'] == 1]
df_2023_goods_1 = df_2023_goods_1[df_2023_goods_1['货款'].notna()]

# Calculate the total amount for each customer
total_amount_by_customer_goods_1 = df_2023_goods_1.groupby('客户名称')['货款'].sum().reset_index()

# Sort the DataFrame by '货款' in descending order and take the top 10 customers
top_10_customers_goods_1 = total_amount_by_customer_goods_1.sort_values(by='货款', ascending=False).head(10)

# Plot the data
plt.figure(figsize=(12, 6))
plt.barh(top_10_customers_goods_1['客户名称'], top_10_customers_goods_1['货款'], color='blue')
plt.xlabel('货款总和')
plt.ylabel('客户名称')
plt.title('货品类型为1的2023年前十名客户货款总和')
plt.grid(axis='x')
plt.show()

top_10_customers_goods_1